package com.sh.data.engine.domain.authority.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sh.data.engine.common.constants.Constants;
import com.sh.data.engine.domain.authority.model.domain.ProjectAuthDomain;
import com.sh.data.engine.domain.authority.service.ProjectAuthService;
import com.sh.data.engine.domain.authority.service.UserProjectService;
import com.sh.data.engine.domain.workspace.index.model.domain.ProjectDomain;
import com.sh.data.engine.domain.workspace.index.model.mapper.ProjectDomainMapper;
import com.sh.data.engine.domain.workspace.index.service.ProjectService;
import com.sh.data.engine.domain.workspace.manager.model.domain.UserDomain;
import com.sh.data.engine.domain.workspace.manager.service.UserService;
import com.sh.data.engine.repository.dao.authority.entity.UserProjectEntity;
import com.sh.data.engine.repository.dao.workspace.index.entity.ProjectEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tinglan.ys
 * @description:
 * @date 2022/8/11 下午5:48
 */
@Service
public class ProjectAuthServiceImpl implements ProjectAuthService {

    @Autowired
    private UserProjectService userProjectService;

    //该id在不同的环境不一样
    @Value("${super.admin.id}")
    private String SUPER_ADMIN;

    @Autowired
    private UserService userService;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ProjectDomainMapper projectDomainMapper;

    @Override
    public IPage<ProjectAuthDomain> getProjectAuth(String userName, Integer pageNum, Integer pageSize) {

//        // 1.分页查询用户列表
//        IPage<UserDomain> userList = userService.getUserListWithoutAdmin(userName, pageNum, pageSize);
//        IPage<ProjectAuthDomain> projectAuthDomainPageResult =
//            userList.convert(e -> Convert.convert(ProjectAuthDomain.class, e));
//
//        for (ProjectAuthDomain projectAuthDomain : projectAuthDomainPageResult.getRecords()) {
//            // 2.查询每个用户拥有的工作空间
//            List<Long> projectIds =
//                    userProjectService
//                            .list(
//                                    new LambdaQueryWrapper<UserProjectEntity>()
//                                            .eq(UserProjectEntity::getUserId, projectAuthDomain.getId()))
//                            .stream()
//                            .map(UserProjectEntity::getProjectId)
//                            .collect(Collectors.toList());
//
//            if (CollectionUtils.isNotEmpty(projectIds)) {
//                // 3.拼装工作空间信息
//                List<ProjectEntity> projectEntityList = projectService.listByIds(projectIds);
//                projectAuthDomain.setProjectEntityList(projectEntityList);
//            }
//        }
//
//        return projectAuthDomainPageResult;
        return null;
    }

    @Override
    public List<ProjectDomain> getProjectList() {
        List<ProjectEntity> list = projectService.findAll();
        return projectDomainMapper.map(list);
    }

    @Override
    public Boolean saveProjectAuth(String userId, List<Long> projectIds) {

        // 先删除原来用户拥有的，再重新保存
        userProjectService.remove(
                new LambdaQueryWrapper<UserProjectEntity>().eq(UserProjectEntity::getUserId, userId));

        List<UserProjectEntity> userProjectEntities = new ArrayList<>();
        for (Long projectId : projectIds) {
            UserProjectEntity userProjectEntity = UserProjectEntity.builder().build();
            userProjectEntity.setUserId(userId);
            userProjectEntity.setProjectId(projectId);
            userProjectEntities.add(userProjectEntity);
        }
        userProjectService.saveBatch(userProjectEntities);
        return true;
    }

    @Override
    public List<ProjectDomain> getProjectAuthByUserId(String userId) {
        if (userId.equals(SUPER_ADMIN)) {
            // 系统管理员拥有所有的空间  tips:拥有系统管理员角色的用户不拥有所有空间，是需要分配的。
            return this.getProjectList();
        }
        List<Long> projectIds =
                userProjectService
                        .list(
                                new LambdaQueryWrapper<UserProjectEntity>()
                                        .eq(UserProjectEntity::getUserId, userId))
                        .stream()
                        .map(UserProjectEntity::getProjectId)
                        .collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(projectIds)) {
            List<ProjectEntity> projectEntityList = projectService.listByIds(projectIds);
            return projectDomainMapper.map(projectEntityList);
        } else {
            return new ArrayList<>();
        }
    }

}
